package com.tbit.uqbike.service.business.impl;

import cn.hutool.core.date.DateUtil;
import com.tbit.common.constant.HandleResultConstant;
import com.tbit.uqbike.object.pojo.UserBlackList;
import com.tbit.uqbike.object.pojo.Usermsg;
import com.tbit.uqbike.object.pojo.statistics.ExportViolationLog;
import com.tbit.uqbike.object.pojo.statistics.ViolationLog;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.service.business.AccountService;
import com.tbit.uqbike.service.business.BlackListLogService;
import com.tbit.uqbike.service.business.UserBlackListService;
import com.tbit.uqbike.service.business.ViolationLogService;
import com.tbit.uqbike.webmanager.dao.core.UsermsgDao;
import com.tbit.uqbike.webmanager.dao.log.ViolationLogDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author: chen
 * @Description: 运维举报日志
 * @Date: 创建于 16:12 2020/3/16
 */
@Service("violationLogService")
public class ViolationLogServiceImpl implements ViolationLogService {

    @Autowired
    private UserBlackListService userBlackListService;
    @Autowired
    private AccountService accountService;
    @Autowired
    private UsermsgDao usermsgDao;
    @Autowired
    private BlackListLogService blackListLogService;

    private static final Map<String, String> types = new HashMap<>();

    static {
        types.put("0", "违规停放");
        types.put("1", "未成年骑行");
        types.put("2", "载人骑行");
        types.put("50", "其他");
    }

    @Autowired
    private ViolationLogDao violationLogDao;

    @Override
    public List<ViolationLog> queryPage(Integer accountId, Integer violationState, Integer reportUserId, String userPhone, String userCode,
                                        Integer violationType, Long startTime, Long endTime, Integer pageNO, Integer rowCount) {
        Map<String, Object> map = new HashMap<>();
        map.put("accountId", accountId);
        map.put("violationState", violationState);
        map.put("reportUserId", reportUserId);
        map.put("userPhone", userPhone);
        map.put("userCode", userCode);
        if (null != startTime) {
            map.put("startTime", new Date(startTime));
        }
        if (null != endTime) {
            map.put("endTime", new Date(endTime));
        }
        map.put("violationType", violationType);
        map.put("pageNO", (pageNO - 1) * rowCount);
        map.put("rowCount", rowCount);
        return violationLogDao.queryPage(map);
    }

    @Override
    public Integer queryCount(Integer accountId, Integer violationState, Integer reportUserId, String userPhone, String userCode, Integer violationType,
                              Long startTime, Long endTime) {
        Map<String, Object> map = new HashMap<>();
        map.put("violationState", violationState);
        map.put("reportUserId", reportUserId);
        map.put("userPhone", userPhone);
        map.put("userCode", userCode);
        if (null != startTime) {
            map.put("startTime", new Date(startTime));
        }
        if (null != endTime) {
            map.put("endTime", new Date(endTime));
        }
        map.put("accountId", accountId);
        map.put("violationType", violationType);
        return violationLogDao.queryCount(map);
    }

    @Override
    public List<ExportViolationLog> export(Integer accountId, Integer violationState, Integer reportUserId, String userPhone, String userCode, Integer violationType, Long startTime, Long endTime) {
        Map<String, Object> map = new HashMap<>();
        map.put("violationState", violationState);
        map.put("reportUserId", reportUserId);
        map.put("userPhone", userPhone);
        map.put("userCode", userCode);
        if (null != startTime) {
            map.put("startTime", new Date(startTime));
        }
        if (null != endTime) {
            map.put("endTime", new Date(endTime));
        }
        map.put("accountId", accountId);
        map.put("violationType", violationType);
        List<ExportViolationLog> exports = violationLogDao.export(map);
        exports.stream().forEach(v -> {
            /*String po = v.getReportPosition();
            if (null != po && !po.equals("")) {
                String[] strings = po.split(",");
                v.setReportPosition(TXHttpClient.getAddress(Double.parseDouble(strings[0]), Double.parseDouble(strings[1])));
            }*/
            v.setViolationType(types.get(v.getViolationType()));
            if ("1".equals(v.getViolationState())) {
                v.setViolationState("已处理");
            } else {
                v.setViolationState("未处理");
            }
        });
        return exports;

    }

    @Override
    public ViolationLog getById(Integer violationLogId) {
        return violationLogDao.getById(violationLogId);
    }

    @Override
    public void updateById(ViolationLog violationLog) {
        violationLogDao.updateById(violationLog);
    }

    @Override
    public void handle(LoginInfo login, Integer violationLogId, Integer handleResult, String handleDesc, Integer type,
                       Date blockTime, String photosUrl) {
        ViolationLog violationLog = new ViolationLog();
        violationLog.setViolationLogId(violationLogId);
        violationLog.setViolationState(1);
        violationLog.setHandleUserId(login.getAccountUserId());
        violationLog.setHandleTime(new Date());
        violationLog.setHandleResult(handleResult == 1 ? HandleResultConstant.PASSED : HandleResultConstant.REFUSED);
        violationLog.setHandleUserName(login.getName());
        violationLog.setHandleDesc(handleDesc);
        ViolationLog violationLogDb = this.getById(violationLogId);
        if (Objects.equals(type, 1)){
            Usermsg userMsg = new Usermsg();
            userMsg.setMsgtype(0);
            userMsg.setUserid(violationLogDb.getUserId());
            userMsg.setCreatetime(new Date());
            userMsg.setMsgstate(0);
            userMsg.setTitle("【温馨提示】");
            userMsg.setContent(handleDesc);
            userMsg.setRemark("管理端发送");
            usermsgDao.insert(userMsg);
            violationLog.setHandleDesc("回复用户，回复内容：" + handleDesc);
            this.updateById(violationLog);
        } else if (Objects.equals(type, 2)){
            Integer brandId = accountService.getBrandId(violationLogDb.getAccountId());
            UserBlackList userBlackList = new UserBlackList();
            userBlackList.setUserId(violationLogDb.getUserId());
            userBlackList.setAccountId(brandId);
            userBlackList.setPhone(violationLogDb.getUserPhone());
            userBlackList.setAddTime(new Date());
            userBlackList.setExpireTime(blockTime);
            userBlackList.setRemark("运维举报拉黑，网页端处理");
            userBlackList.setPhotosUrl(photosUrl);
            UserBlackList userBlackListDb = userBlackListService.getByUserId(violationLogDb.getUserId());
            if (userBlackListDb != null){
                UserBlackList userBlackListUpdate = new UserBlackList();
                userBlackListUpdate.setUserId(violationLogDb.getUserId());
                userBlackListUpdate.setExpireTime(blockTime);
                userBlackListUpdate.setRemark("运维举报拉黑，网页端处理");
                userBlackListUpdate.setPhotosUrl(photosUrl);
                userBlackListService.updateEntityByUserId(userBlackListUpdate);
            }else {
                userBlackListService.insert(userBlackList,0);
            }
            blackListLogService.insert(login, userBlackList);
            violationLog.setHandleDesc("拉黑用户，到期时间：" + DateUtil.formatDate(blockTime));
            this.updateById(violationLog);
        } else {
            this.updateById(violationLog);
        }
    }
}
